<?php
use yii\helpers\Url;

$web = Url::base();
?>

<script type="application/javascript" src="<?= $web ?>/js/icheck-init.js"></script>
<script>

    var orderList = new Array();
    var institutionCode = 0;
    var isSendIntegral = 1;
    var institutionName = '';
    var insSelect3;  //导入页面机构下拉
    var importType = 1;

    $(function ($) {
        $("#step1").show();
        $("#step2").hide();
        $("#step3").hide();
        $("#isSendIntegral").bootstrapSwitch('state', true);
        $("#preStep").attr("disabled", false);
        $("#orderOnly").iCheck('check');
        $("#orderForPay").iCheck('uncheck');
        $("#orderForSendIntegral").iCheck('uncheck');
        //导入页面机构下拉列表内容初始化
        var institution =  <?= json_encode($institutionList) ?>;
        insSelect3 = $('#select3').treeAutocomplete({
            defaultText: '----请选择机构----',
            source: institution
        });
        insSelect3.setValue("<?= $current_institution_code ?>");
        $("#importlist").css("display", "none");
        $("#importlist").contents().find("tbody").html("");

    });

    //上一步下一步
    function nextStep(step) {

        switch (step) {

            //选择机构
            case 1:
                $("#step1").show();
                $("#step2").hide();
                break;

            //下载excel
            case 2:
                if ($("#select3").val() == 0) {
                    $("#alert3").css("display", "block");
                }
                else {
                    $("#step2").show();
                    $("#step1").hide();
                    $("#step3").hide();
                }
                break;


            //上传excel
            case 3:
                institutionCode = $("#select3").val();
                institutionName = insSelect3.getText();

                if ($("#orderForSendIntegral").is(':checked') == true) {
                    importType = 3;
                } else if ($("#orderForPay").is(':checked') == true) {
                    importType = 2;
                } else {
                    importType = 1;
                }

                $("#step3").show();
                $("#step2").hide();

                $('#upload_image_submit_form').attr('action', "import-order-handler?institutionCode=" + institutionCode + "&importType=" + importType);
                break;

            default:
                break;
        }
    }

    //上传excel处理
    function uploadClick() {

        var data = {};
        if ($("#upload_image").val() != '') {
            $("#upload_image_submit_form").submit();
            loading.show();

            $("#upload_exec_target").load(function () {
                try {
                    $("#importlist").contents().find("tbody").html("");
                    var result = eval('(' + ($("#upload_exec_target").contents().find("body").html()) + ')');
                    if (result.status == 0 && result.data.iCount > 0) {
                        text = "导入成功！【" + institutionName + "】机构导入订单数据：" + result.data.iCount + "条。合计金额:" + result.data.amount + '元。';
                        if (importType == 2) {
                            text = text + "导入方式：生成订单后扣除用户积分。";
                        } else if (importType == 3) {
                            text = text + "导入方式：先发放等值积分，生成订单后再扣除用户积分。";
                        } else {
                            text = text + "导入方式：仅生成订单。";
                        }
                        text = text + "  请点击【生成订单】生成相关订单。";
                        $(".two-error").text(text);

                        orderList = result.data.right_data;
                        var textlist = '';
                        var re = result.data.right_data;
                        $.each(re, function (i, item) {
                            textcontent = "<tr><td>" + item.goodCode +"</td><td>" + item.num + "</td><td>" + item.name + "</td><td>" + item.province + "</td><td>" + item.city + "</td><td>" + item.region + "</td><td>" + item.address + "</td><td>" + item.phone + "</td><td>" + item.email + "</td><td>" + item.postCodes + "</td><td>" + item.orderRemark + "</td><td>" + item.idcardName + "</td><td>" + item.idcardNumber +  "</td></tr>";
                            textlist = textlist + textcontent;
                        });

                        $("#importlist").css("display", "block");
                        $("#importlist").contents().find("tbody").prepend(textlist);
                        $("#confirm").attr("disabled", false);
                        $("#preStep").attr("disabled", true);

                    }
                    else if (result.status == 2) {
                        var re = eval('(' + result.message + ')');
                        $(".two-error").text(re);
                    }
                    else if (result.status == 1) {
                        var re = eval('(' + result.message + ')');
                        text = "导入失败！手机号码：" + re[0]["phone"] + "，姓名：" + re[0]["name"] + "导入内容不正确：" + re[0]["error"] + "请修改后重新导入！";
                        $(".two-error").text(text);
                    }
                    else {
                        var re = "导入失败，请检查文件的正确性！";
                        $(".two-error").text(re);
                    }

                } catch (err) {
                    var re = $("#upload_exec_target").contents().find("h2").html();
                    $(".two-error").text(re);
                }
                $("#upload_image").val('');
                loading.hide();

            });
        }

    }

    //确认生成订单
    function confirm() {
        loading.show();
        var data = {};
        data.institutionCode = institutionCode;
        //data.isSendIntegral = isSendIntegral;
        data.importType = importType;
        data.orderList = orderList;

        $("#confirm").attr("disabled", true);
        requestJson("import-order-confirm-handler", data, true, function (re) {
            alert("订单生成成功！");
            $("#confirm").attr("disabled", true);
            location.reload();
        });
    }


</script>
<!-- 导入订单-->


<div class="modal-dialog" id="step1" style="display:none">
    <div class="modal-content">
        <div class="modal-header">
            <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
            <h4 class="modal-title">确认机构</h4>
        </div>
        <div class="modal-body">
            <div class="form-horizontal adminex-form">
                <div class="form-group">
                    <label class="col-sm-2 control-label">所属机构</label>
                    <div class="col-sm-10">
                        <div id="select3"></div>
                        <label id='alert3' style="display: none;"><span style="color: red">请选择一个机构</span></label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 col-sm-3 control-label">导入方式</label>
                    <div class="col-lg-10">
                        <label class="control-label">
                            <div class="square-blue">
                                <div class="radio">
                                    <div class="square-blue form-inline icheck">
                                        <div class="radio popovers">
                                            <input type="radio" name="optionsRadios" id="orderOnly" value="1">
                                            仅生成订单
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </label>
                    </div>
                    <label class="col-lg-2 col-sm-3 control-label"> </label>
                    <div class="col-lg-10">
                        <label class="control-label">
                            <div class="square-blue">
                                <div class="radio">
                                    <div class="square-blue form-inline icheck">
                                        <div class="radio popovers">
                                            <input type="radio" name="optionsRadios" id="orderForPay" value="2">
                                            生成订单后扣除用户积分
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </label>
                    </div>
                    <label class="col-lg-2 col-sm-3 control-label"> </label>
                    <div class="col-lg-10">
                        <label class="control-label">
                            <div class="square-blue">
                                <div class="radio">
                                    <div class="square-blue form-inline icheck">
                                        <div class="radio popovers">
                                            <input type="radio" name="optionsRadios" id="orderForSendIntegral"
                                                   value="3">
                                            先发放等值积分，生成订单后再扣除用户积分
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </label>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal">关闭</button>
            <button class="btn btn-primary" onclick="nextStep(2)">下一步</button>
        </div>
    </div>
</div>
<div class="modal-dialog" id="step2" style="display:none">
    <div class="modal-content">
        <div class="modal-header">
            <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
            <h4 class="modal-title">下载excel</h4>
        </div>
        <div class="modal-body">

            <div class="form-group">
                <a id="excel-download" href="<?= $web ?>/orderImportDemo.xlsx">
                    <img src="<?= $web ?>/images/hair-two-1.png">【点我】下载excel模板
                </a>
            </div>

        </div>
        <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal">关闭</button>
            <button class="btn btn-default" onclick="nextStep(1)">上一步</button>
            <button class="btn btn-primary" onclick="nextStep(3)">下一步</button>
        </div>
    </div>
</div>
<div class="modal-dialog modal-lg" id="step3" style="display:none">
    <div class="modal-content">
        <div class="modal-header">
            <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
            <h4 class="modal-title">上传excel</h4>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <div class="two-center">
                    <div class="two-words two-error-none">请点击【选择文件】导入EXCEL!<span
                            style="color: red">注意：每次导入记录数不能超过5000条！</span>导入的时间可能很长，请耐心等待！
                    </div>
                    <div class="two-error" style="color: red"></div>
                </div>
            </div>
            <div class="form-group">
                <form id="upload_image_submit_form" enctype="multipart/form-data" method="post"
                      action="import-order-handler" target="upload_exec_target">
                    <input id="upload_image" name="file" type="file" onchange="uploadClick()"/>
                    <input type="hidden" name="dir"/>
                    <img src="<?= $web ?>/images/hair-two-1.png">
                </form>
                <iframe id="upload_exec_target" name="upload_exec_target" style="display: none;"></iframe>
            </div>
            <div class="form-group table-responsive" id="importlist" style="display: none">
                <table class="table table-bordered table-hover table-condensed">
                    <thead>
                    <tr>
                        <th>产品代码</th>
                        <th>数量</th>
                        <th>收货人姓名</th>
                        <th>省份</th>
                        <th>市</th>
                        <th>区</th>
                        <th>详细地址</th>
                        <th>手机号码</th>
                        <th>电子邮件</th>
                        <th>邮政编码</th>
                        <th>订单备注</th>
                        <th>身份证姓名</th>
                        <th>身份证号码</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>

        </div>
        <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal">关闭</button>
            <button class="btn btn-default" id="preStep" onclick="nextStep(2)">上一步</button>
            <button class="btn btn-primary" id="confirm" disabled="true" onclick="confirm()">生成订单</button>
        </div>
    </div>
</div>


